package black.market.main;

import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import black.market.database.DatabaseActivity;
import black.market.main.BlackPublic.Action;
import black.market.weather.WeatherInfoActivity;

public class BlackMarketActivity extends ListActivity {
	public static final String TAG = "BlackMarketActivity";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		String[] menuList = getResources().getStringArray(R.array.main_menu_list);
		setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_line_item1, menuList));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		switch (position) {
		case 0:
			startActivity(new Intent(getApplicationContext(), WeatherInfoActivity.class));
			break;

		case 1:
			startActivity(new Intent(getApplicationContext(), DatabaseActivity.class));
			// PackageInfo packageInfo = new
			// PackageInfo(getApplicationContext(), getPackageManager());
			// packageInfo.setPackageInfo(PackageManager.GET_URI_PERMISSION_PATTERNS
			// | PackageManager.GET_PROVIDERS | PackageManager.GET_GIDS
			// | PackageManager.GET_PERMISSIONS);
			// if (packageInfo.getPermissions() != null) {
			// packageInfo.selectPermissions(packageInfo.getPermissions());
			// }
			break;

		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	public void check_Alarm_Preference() {
		AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		Intent intent = null;
		PendingIntent mPendingIntent = null;
		SharedPreferences preferences = getSharedPreferences(Action.SHARED_PREF.toString(), MODE_PRIVATE);
		boolean isLocation = preferences.getBoolean(Action.READLOCATION_1H.toString(), false);
		boolean isKmaData = preferences.getBoolean(Action.KMA_DATA_INTERVAL3H.toString(), false);
		if (isKmaData) {
			BlackPublic.identifiesLog(TAG, "kmaAlarm3Hour_Repeating -> ");
			intent = new Intent(Action.KMA_DATA_INTERVAL3H.toString());
			mPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_HOUR * 2, (AlarmManager.INTERVAL_HOUR * 3) + 150000, mPendingIntent);
			SharedPreferences.Editor editor = preferences.edit();
			editor.putBoolean(Action.KMA_DATA_INTERVAL3H.toString(), true);
			editor.commit();
		}
		if (isLocation) {
			BlackPublic.identifiesLog(TAG, "kmaAlarm3Hour_Repeating -> ");
			intent = new Intent(Action.READLOCATION_1H.toString());
			mPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), AlarmManager.INTERVAL_HOUR, mPendingIntent);
			SharedPreferences.Editor editor = preferences.edit();
			editor.putBoolean(Action.READLOCATION_1H.toString(), true);
			editor.commit();
		}
	}

	public void readLocationAlarm1Hour_Repeating() {
		BlackPublic.identifiesLog(TAG, "readLocationAlarm1Hour_Repeating -> isReadLocationAlarm : ");
		Intent intent = new Intent();
		intent.setAction(Action.READLOCATION_1H.toString());
		PendingIntent mPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		BlackPublic.debugLog(TAG, "mPendingIntent:" + mPendingIntent.getTargetPackage());
		AlarmManager mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
		mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, mPendingIntent);
	}

	public void kmaAlarm3Hour_Repeating() {
		BlackPublic.identifiesLog(TAG, "kmaAlarm3Hour_InexactRepeating -> isKmaAlarm : ");
		Intent intent = new Intent();
		intent.setAction(Action.KMA_DATA_INTERVAL3H.toString());
		PendingIntent mPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
		mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_HOUR * 2, 30000, mPendingIntent);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

}